
#include "screen_protection.h"
#include "page_base.h"
#include "img_common.h"
#include "sys_scene.h"
#include "pt_ipc.h"
#include "pt_info.h"
#include "sys_msg.h"
#include "sys_font.h"
#include "pt_fs.h"

ui_sp_widget_t u_spw;

static void screen_protection_event_callback(lv_event_t* event)
{
    switch(event->code){
    case LV_EVENT_CLICKED:
    {
        debug_d("clicked\n");
        root_exit_idle();
        
        break;
    }
    default:break;
    }
}


void ui_init_screen_protection_page_src(lv_obj_t *superview)
{
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    debug_d("create \n");

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);

    u_spw.bg  = lv_obj_create(superview);

    lv_obj_set_style_radius(u_spw.bg, 0, LV_PART_MAIN);
	lv_obj_set_style_border_width(u_spw.bg, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(u_spw.bg, LV_OPA_0, LV_PART_MAIN);
	lv_obj_set_style_border_color(u_spw.bg, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
    lv_obj_set_style_pad_all(u_spw.bg,0,0);

    #if 0
	lv_obj_set_style_bg_opa(u_spw.bg, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(u_spw.bg, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(u_spw.bg, lv_color_make(0,0,0), LV_PART_MAIN);
    #endif

	lv_obj_set_pos(u_spw.bg,0,0);
	lv_obj_set_size(u_spw.bg,pw,ph);

    lv_obj_add_event_cb(u_spw.bg, screen_protection_event_callback,LV_EVENT_ALL, NULL);
    //****************************************************** */

    u_spw.img_weather = lv_img_create(superview);
    lv_img_set_src(u_spw.img_weather,pt_fs_get(pt_weather_icon_get(pt_weather_get())));
    lv_obj_set_pos(u_spw.img_weather,0,0);

    u_spw.label_hour = lv_label_create(superview);
    lv_obj_set_style_text_font(u_spw.label_hour, lv_font_get(80), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(u_spw.label_hour,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_opa(u_spw.label_hour,LV_OPA_90,0);
    lv_label_set_text_fmt( u_spw.label_hour, "%02d", pt_hour_get());
    lv_obj_set_pos(u_spw.label_hour,205,58);

    u_spw.label_colon = lv_label_create(superview);
    lv_obj_set_style_text_font(u_spw.label_colon, lv_font_get(80), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(u_spw.label_colon,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_opa(u_spw.label_colon,LV_OPA_90,0);
    lv_label_set_text_fmt( u_spw.label_colon, ":");
    lv_obj_set_pos(u_spw.label_colon,300,58);

    u_spw.label_min = lv_label_create(superview);
    lv_obj_set_style_text_font(u_spw.label_min, lv_font_get(80), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(u_spw.label_min,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_opa(u_spw.label_min,LV_OPA_90,0);
    lv_label_set_text_fmt( u_spw.label_min, "%02d", pt_minute_get());
    lv_obj_set_pos(u_spw.label_min,325,58);

    u_spw.label_date = lv_label_create(superview);
    lv_obj_set_style_text_font(u_spw.label_date, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(u_spw.label_date,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_opa(u_spw.label_date,LV_OPA_80,0);
    lv_label_set_text_fmt( u_spw.label_date, "%02d/%02d %s", pt_month_get(),pt_day_get(),pt_week_txt_get(pt_week_get()));
    lv_obj_set_pos(u_spw.label_date,218,166);

    u_spw.label_tempera = lv_label_create(superview);
    lv_obj_set_style_text_font(u_spw.label_tempera, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(u_spw.label_tempera,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_obj_set_style_text_opa(u_spw.label_tempera,LV_OPA_80,0);
    lv_label_set_text_fmt( u_spw.label_tempera, "室外%d°C", pt_temperature_get());
    lv_obj_set_pos(u_spw.label_tempera,345,166);

    u_spw.img_message = lv_img_create(superview);
    lv_img_set_src( u_spw.img_message, pt_fs_get("News.png"));
    lv_obj_set_pos(u_spw.img_message,218,208);

    u_spw.label_message = lv_label_create(superview);
    lv_obj_set_style_text_font(u_spw.label_message, lv_font_get(20), LV_STATE_DEFAULT );
    lv_obj_set_style_text_color(u_spw.label_message,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN);
    lv_label_set_text_fmt(u_spw.label_message, "%d条未阅读信息",msg_info_get_noread());
    lv_obj_set_pos(u_spw.label_message,257,212);

    if(msg_info_get_noread()){
        if(u_spw.img_message && lv_obj_has_flag(u_spw.img_message,LV_OBJ_FLAG_HIDDEN)){
            lv_obj_clear_flag(u_spw.img_message,LV_OBJ_FLAG_HIDDEN);  
        }
        if(u_spw.label_message && lv_obj_has_flag(u_spw.label_message,LV_OBJ_FLAG_HIDDEN)){
            lv_obj_clear_flag(u_spw.label_message,LV_OBJ_FLAG_HIDDEN);  
        }
    }else{
        if(u_spw.img_message){
            lv_obj_add_flag(u_spw.img_message,LV_OBJ_FLAG_HIDDEN);  
        }
        if(u_spw.label_message){
            lv_obj_add_flag(u_spw.label_message,LV_OBJ_FLAG_HIDDEN);  
        }
    }

    u_spw.timer_sp_refresh = lv_timer_create(timer_sp_refresh_cb, 2000,NULL);
}

void timer_sp_refresh_cb(lv_timer_t * timer)
{
    static bool two_val = 0;
    if(two_val)
        lv_label_set_text_fmt(u_spw.label_colon, ":");
    else
        lv_label_set_text_fmt(u_spw.label_colon, " ");
    two_val = !two_val;

    get_sys_time();
    static int min = 0;
    if(min != pt_minute_get())
    {
        lv_label_set_text_fmt(u_spw.label_hour, "%02d", pt_hour_get());
        lv_label_set_text_fmt(u_spw.label_min, "%02d", pt_minute_get());
        min = pt_minute_get();
    }

    get_sys_date();
    static int day = 0;
    if(day != pt_day_get())
    {
        lv_label_set_text_fmt( u_spw.label_date, "%02d/%02d %s", pt_month_get(),pt_day_get(), pt_week_txt_get(pt_week_get()));
        day = pt_day_get();
    }

    static int temper = 0;
    if(temper != pt_temperature_get())
    {
        lv_label_set_text_fmt( u_spw.label_tempera, "室外%d°C", pt_temperature_get());
        temper = pt_temperature_get();
    }
}

void ui_release_screen_protection_page_src(lv_obj_t *superview)
{
    if(u_spw.bg){
        lv_obj_del(u_spw.bg);
        u_spw.bg = NULL;
    }

    if(u_spw.img_weather){
        lv_obj_del(u_spw.img_weather);
        u_spw.img_weather = NULL;
    }

    if(u_spw.label_hour){
        lv_obj_del(u_spw.label_hour);
        u_spw.label_hour = NULL;
    }

    if(u_spw.label_colon){
        lv_obj_del(u_spw.label_colon);
        u_spw.label_colon = NULL;
    }

    if(u_spw.label_min){
        lv_obj_del(u_spw.label_min);
        u_spw.label_min = NULL;
    }

    if(u_spw.timer_sp_refresh){
        lv_timer_del(u_spw.timer_sp_refresh);
        u_spw.timer_sp_refresh = NULL;
    }

    if(u_spw.label_date){
        lv_obj_del(u_spw.label_date);
        u_spw.label_date = NULL;
    }

    if(u_spw.label_tempera){
        lv_obj_del(u_spw.label_tempera);
        u_spw.label_tempera = NULL;
    }

    if(u_spw.img_message){
        lv_obj_del(u_spw.img_message);
        u_spw.img_message = NULL;
    }

    if(u_spw.label_message){
        lv_obj_del(u_spw.label_message);
        u_spw.label_message = NULL;
    }
}

static void on_page_screen_protection_view_did_load(page_base_t* self)
{
    ui_init_screen_protection_page_src(self->root);
}

static void on_scene_page_view_did_unload(page_base_t * self)
{
    ui_release_screen_protection_page_src(self->root);
}

static screen_protection_on_custom_attr_config(page_base_t* self)
{}
static screen_protection_on_view_load(page_base_t* self)
{}
static screen_protection_on_view_will_appear(page_base_t* self)
{}
static screen_protection_on_view_did_appear(page_base_t* self)
{}
static screen_protection_on_view_will_disappear(page_base_t* self)
{}
static screen_protection_on_view_did_disappear(page_base_t* self)
{}

static page_vtable_t page_param = {
    .on_custom_attr_config  = screen_protection_on_custom_attr_config,
    .on_view_load           = screen_protection_on_view_load,
    .on_view_did_load       = on_page_screen_protection_view_did_load,
    .on_view_will_appear    = screen_protection_on_view_will_appear,
    .on_view_did_appear     = screen_protection_on_view_did_appear,
    .on_view_will_disappear = screen_protection_on_view_will_disappear,
    .on_view_did_disappear  = screen_protection_on_view_did_disappear,
    .on_view_did_unload     = on_scene_page_view_did_unload,
};

page_vtable_t* lv_page_screen_protection_create(void)
{
    return &page_param;
}
